import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:flutter_reactive_layout_demo/ui/components/reactive_navigation_scaffold.dart';
import 'package:flutter_reactive_layout_demo/ui/components/rective_navigation_destination.dart';
import 'package:universal_platform/universal_platform.dart';

class ReactiveNavigationRootLayout extends StatelessWidget {
  const ReactiveNavigationRootLayout({
    super.key,
    required this.child,
    required this.currentIndex,
  });

  final Widget child;
  final int currentIndex;
  static const _switcherKey = ValueKey('switcherKey');
  static const _navigationRailKey = ValueKey('navigationRailKey');

  @override
  Widget build(BuildContext context) {
    List<NavigationDestination> destinations = RectiveNavigationDestination.destinations.map((e){
      return NavigationDestination(icon: e.icon, label: e.label);
    }).toList();

    return ReactiveNavigationScaffold(
      key: _navigationRailKey,
      destinations: destinations,
      selectedIndex: currentIndex,
      onDestinationSelected: (index) {
        final destination = RectiveNavigationDestination.destinations[index];
        GoRouter.of(context).go(destination.route);
      },
      child: Column(
        children: [
          Expanded(
            child: _Switcher(
              key: _switcherKey,
              child: child,
            )
          )
        ],
      ),
    );
  }
}

class _Switcher extends StatelessWidget {
  final Widget child;

  const _Switcher({
    required this.child,
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    return UniversalPlatform.isDesktop
        ? child
        : AnimatedSwitcher(
            key: key,
            duration: const Duration(milliseconds: 200),
            switchInCurve: Curves.easeInOut,
            switchOutCurve: Curves.easeInOut,
            child: child,
      );
  }
}